/**
 * 根据屏幕大小获取字体大小
 * @param fontsize
 * @returns
 */
export const transformFontSize = (fontsize: number) => {
  // 获取屏幕宽度
  const width = document.body.scrollWidth;
  const ratio = width / 1920;
  return Math.ceil(fontsize * ratio);
};

/**
 * 生成随机的浅色系颜色列表
 * @param num  生成的颜色个数
 * @returns 颜色列表
 */
export const getRandomHexColor = (num: number = 1) => {
  const colors = [];
  for (let i = 0; i < num; i++) {
    const hue = Math.floor(Math.random() * 200) + 100; // 色相
    const saturation = Math.floor(Math.random() * 30) + 70; // 低饱和度，范围70% - 100%
    const lightness = Math.floor(Math.random() * 20) + 80; // 高亮度，范围80% - 100%
    const hsl = `hsl(${hue}%, ${saturation}%, ${lightness}%)`;
    console.log(hsl);
    colors.push(tinycolor(hsl).toHexString());
  }
  return colors;
};

/**
 * 根据传入的颜色值生成颜色梯度
 * @param color 基础色
 * @param mode 返回的颜色格式
 * @returns 颜色数组
 */
export const getColorGradient = (
  color: string,
  mode: "hex" | "rgb" = "hex"
) => {
  const colors: string[] = [];
  const modeFun = mode === "hex" ? "toHex8String" : "toRgbString";
  const baseColor = tinycolor(color);
  const baseColorRgb = tinycolor(color).toRgb();
  const resetR =
    baseColorRgb.r > 200
      ? 255 - baseColorRgb.r
      : baseColorRgb.r > 100
        ? 248 - baseColorRgb.r
        : 232 - baseColorRgb.r;
  const resetG =
    baseColorRgb.g > 200
      ? 250 - baseColorRgb.g
      : baseColorRgb.g < 100
        ? 244 - baseColorRgb.g
        : 248 - baseColorRgb.g;
  const resetB =
    baseColorRgb.b > 100 ? 250 - baseColorRgb.b : 232 - baseColorRgb.b;

  const resetR2 =
    baseColorRgb.r > 200
      ? baseColorRgb.r - 77
      : baseColorRgb.r < 100
        ? baseColorRgb.r - 0
        : baseColorRgb.r - 22;
  const resetG2 =
    baseColorRgb.g > 100
      ? baseColorRgb.g - 14
      : baseColorRgb.g < 100
        ? baseColorRgb.g - 17
        : baseColorRgb.g;
  const resetB2 =
    baseColorRgb.b > 100
      ? baseColorRgb.b - 77
      : baseColorRgb.b > 60
        ? baseColorRgb.b - 10
        : baseColorRgb;

  for (let i = 1; i <= 5; i++) {
    const newColor = {
      r: baseColorRgb.r + i * Math.ceil(resetR / 5),
      g: baseColorRgb.g + i * Math.ceil(resetG / 5),
      b: baseColorRgb.b + i * Math.ceil(resetB / 5),
    };
    colors.unshift(tinycolor(newColor)[modeFun]());
  }
  colors.push(baseColor[modeFun]());
  for (let i = 1; i <= 4; i++) {
    const newColor = {
      r: baseColorRgb.r - i * Math.floor(resetR2 / 4),
      g: baseColorRgb.g - i * Math.floor(resetG2 / 4),
      b: baseColorRgb.b - Math.floor(resetB2 / 4) * i,
    };
    colors.push(tinycolor(newColor)[modeFun]());
  }
  return colors;
};

/**
 * 获取num1-num2之间的随机数
 * @param num1
 * @param num2
 * @returns 随机数
 */
export const getRandomNumber = (num1: number, num2: number) => {
  return Math.floor(Math.random() * (num2 + 1 - num1)) + num1;
};

/**
 * 根据基础色计算它的对比色
 * @param hex 基础色
 * @returns 颜色值
 */
export const getContrastingColor = (hex: string) => {
  // 计算对比色
  const { r, g, b } = tinycolor(hex).toRgb();
  // 这里简单地计算对比色
  return tinycolor({ r: 255 - r, g: 255 - g, b: 255 - b }).toHexString();
};

/**
 * 判断是否为空对象
 * @param obj 对象
 * @returns
 */
export const objIsEmpty = (obj: Record<string, any>) => {
  return Object.keys(obj).length === 0 && obj.constructor === Object;
};

/**
 * hex色值转为hsl
 * @param hex
 * @returns
 */
const hexToHSL = (hex: string) => {
  // 移除 # 号
  hex = hex.replace("#", "");

  // 将 hex 转换为 RGB
  const r = parseInt(hex.substring(0, 2), 16) / 255;
  const g = parseInt(hex.substring(2, 4), 16) / 255;
  const b = parseInt(hex.substring(4, 6), 16) / 255;

  const max = Math.max(r, g, b);
  const min = Math.min(r, g, b);
  let h,
    s,
    l = (max + min) / 2;

  if (max === min) {
    h = s = 0;
  } else {
    const d = max - min;
    s = l > 0.5 ? d / (2 - max - min) : d / (max + min);

    switch (max) {
      case r:
        h = (g - b) / d + (g < b ? 6 : 0);
        break;
      case g:
        h = (b - r) / d + 2;
        break;
      case b:
        h = (r - g) / d + 4;
        break;
    }
    h /= 6;
  }

  return { h: h * 360, s: s * 100, l: l * 100 };
};

/**
 * hsl转为hex
 * @param h
 * @param s
 * @param l
 * @returns
 */
const hslToHex = (h: number, s: number, l: number) => {
  l /= 100;
  const a = (s * Math.min(l, 1 - l)) / 100;
  const f = (n: number) => {
    const k = (n + h / 30) % 12;
    const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
    return Math.round(255 * color)
      .toString(16)
      .padStart(2, "0");
  };
  return `#${f(0)}${f(8)}${f(4)}`;
};

/**
 * 根据提供的颜色生成色板
 * @param baseColor
 * @returns
 */
export const generateColorPalette = (baseColor: string) => {
  const hsl = hexToHSL(baseColor);
  const { h, s, l } = hsl;

  // 生成浅色系列（提高亮度，降低饱和度）
  const lightColors = [
    hslToHex(h - 8, 100, 95), // 最浅
    hslToHex(h - 6, s - 4, 90),
    hslToHex(h - 4, Math.max(s - 5, 0), 85),
    hslToHex(h - 2, s, 80),
    hslToHex(h, s, l), // 最深的浅色
  ];

  // 生成深色系列（降低亮度，保持或略微调整饱和度）
  const darkColors = [
    hslToHex(h, Math.min(s + 5, 100), 45), // 最浅的深色
    hslToHex(h, Math.min(s + 10, 100), 35),
    hslToHex(h, Math.min(s + 15, 100), 25),
    hslToHex(h, Math.min(s + 20, 100), 15), // 最深
  ];
  console.log([...lightColors, ...darkColors]);
  return [...lightColors, ...darkColors];
};

/**
 * 元素全屏
 */
export const fullScreen = () => {
  const element = document.querySelector("#fullEle");
  if (element?.requestFullscreen) {
    element.requestFullscreen();
  }

  // else if (element?.mozRequestFullScreen) {
  //   /* Firefox */
  //   element?.mozRequestFullScreen();
  // } else if (element?.webkitRequestFullscreen) {
  //   /* Chrome, Safari & Opera */
  //   element?.webkitRequestFullscreen();
  // } else if (element?.msRequestFullscreen) {
  //   /* IE/Edge */
  //   element?.msRequestFullscreen();
  // }
};
